import Block from "../base/block";
import { winH, winW} from "../libs/utils";
import Img from "../base/img";
import {Rect} from "../base/shape";


/**
 * 相册弹窗view
 */
export default class AlbumView extends Block {

    constructor(x, y, width, height, imgList = []) {
        super(x, y, width, height);
        this.bgColor ='rgba(0,0,0,0.5)';
        this.imgList = imgList;
        this.index = 0;
        this.callback = null;
        if (imgList.length >= 1) {
            this.imageSrc = imgList[0];
        } else {
            this.imageSrc = null;
        }
        // var dialog = new Block(20, winH / 2 - 40, winW - 40, 60);

        var rect = new Rect(0, winH / 2 - (winW ) / 2, winW , winW , 10);
        // var rect = new Rect(10, winH / 2 - (winW - 20) / 2, winW - 20, winW - 20, 10);
        // rect.bgColor = 'white';
        var closeBtn = new Img("js/sub/images/icon_close.png", winW-60, winH / 2 - (winW ) / 2-50, 40 , 40 , 40 , 40 , "contain", 'center');


        var arrowLeft = new Img("js/sub/images/arrow_left.png", 40, winH / 2 + (winW) / 2+10, winW / 2 - 80, 90);
        var arrowRight = new Img("js/sub/images/arrow_right.png", winW / 2 + 20, winH / 2 + (winW) / 2+10, winW / 2 - 80, 90);

        // var imagePost = new Img(this.imgList[this.index], 10, winH / 2 - (winW - 20) / 2, winW - 20, winW - 20, winW - 20, winW - 20, "contain", 'center');
        this.imagePost = new Img(this.imgList[this.index], 0, winH / 2 - (winW ) / 2, winW , winW , winW , winW , "contain", 'center');


        this.addChild(closeBtn, "closeBtn")
        this.addChild(rect, "rect")

        this.addChild(arrowLeft, "arrowLeft")
        this.addChild(arrowRight, "arrowRight")
        this.addChild(this.imagePost, "imagePost")
        // this.addChild(dialog, "dialog")

        arrowLeft.bindClickEvent(() => {
            if (this.index === 0) {
                wx.showToast({
                    title: '翻到头了',
                    icon:'none'
                });
                return;
            }
            this.index -= 1;
            this.imagePost.imgSrc = this.imgList[this.index]
        })


        arrowRight.bindClickEvent(() => {

            if (this.index === (this.imgList.length - 1)) {

                wx.showToast({
                    title: '最后一张啦',
                    icon:'none'
                });
                return;
            }
            this.index += 1;
            console.log(` ${this.imgList.length}，${imgList.lengtutish} ${this.index}当前显示的图片  ${this.imgList[this.index]}`)
            this.imagePost.imgSrc = this.imgList[this.index]
        })
        closeBtn.bindClickEvent(() => {
            if (this.callback) {
                this.callback();
            }
        })
    }
    setImgList(imgList){
        this.imgList = imgList;
        this.index=0;
        this.imagePost.imgSrc = imgList[this.index]
    }
    setCallback(callback) {
        this.callback = callback;
    }

}
